<template>
    <div>
        <el-form :model="ruleFormView" label-width="120px" class="demo-ruleForm">
            <div class="container">
                <h4 style="margin-bottom: 20px">仓库信息</h4>
                <el-row>
                    <el-col :span="12">
                        <el-form-item label="仓库名称：">
                            {{ruleFormView.name}}
                        </el-form-item>
                        <el-form-item label="仓储方式：">
                            {{ruleFormView.storeMethod}}
                        </el-form-item>
                    </el-col>
                    <el-col :span="12">
                        <el-form-item label="仓库地址：">
                            {{ruleFormView.address}}
                        </el-form-item>
                        <el-form-item label="仓储条件：">
                            {{ruleFormView.storeCondition}}
                        </el-form-item>
                    </el-col>
                </el-row>
            </div>
            <div class="container" style="margin-top: 20px">
                <h4 style="margin-bottom: 20px">库存信息</h4>
                <el-row style="margin-bottom:20px">
                    <el-col :span="7">
                        <el-input placeholder="请输入关键词" v-model.trim="searchName1">
                            <i slot="prefix" class="el-input__icon el-icon-search"></i>
                            <el-button slot="append" @click="handleSearch1">搜索</el-button>
                        </el-input>
                    </el-col>
                </el-row>
                <el-row>
                    <el-table
                        :data="tableData"
                        border
                        ref="multipleTable"
                        header-cell-class-name="table-header"
                    >
                        <el-table-column prop="materialType" sortable label="类型"></el-table-column>
                        <el-table-column prop="breedName" sortable label="名称"></el-table-column>
                        <el-table-column prop="batch" sortable label="批次"></el-table-column>
                        <el-table-column prop="totalNum" sortable label="数量">
                            <template slot-scope="scope">
                                {{scope.row.totalNum}}{{scope.row.units}}
                            </template>
                        </el-table-column>
                        <el-table-column prop="type" sortable label="入库类型">
                            <template slot-scope="scope">
                                {{scope.row.type}}入库
                            </template>
                        </el-table-column>
                        <el-table-column prop="surplusNum" sortable label="剩余数量">
                            <template slot-scope="scope">
                                {{scope.row.surplusNum}}{{scope.row.units}}
                            </template>
                        </el-table-column>
                        <el-table-column label="操作" width="150" align="center">
                            <template slot-scope="scope">
                                <el-button
                                    type="text"
                                    @click="handleView(scope.$index, scope.row)"
                                >查看</el-button>
                                <el-button
                                    type="text"
                                    @click="handleFrmLoss(scope.$index, scope.row)"
                                    v-if="scope.row.type == '原料'"
                                >报损</el-button>
                                <el-button
                                    type="text"
                                    @click="handleNum(scope.$index, scope.row)"
                                    v-if="scope.row.type == '生产'"
                                >调整产量</el-button>
                            </template>
                        </el-table-column>
                    </el-table>
                    <el-pagination layout="prev, pager, next" :page-size="pagesize1" @current-change="handleCurrentChange1" :total="total1" :current-page.sync="currentPage1" style="margin-top:20px"></el-pagination>
                </el-row>
            </div>
            <el-form-item class="footerFixed">
                <el-button @click="backPage">返回</el-button>
            </el-form-item>
        </el-form>
        <el-dialog title="报损" :visible.sync="dialogFormVisible">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm">
                <el-row>
                    <el-col :span="16">
                        <el-form-item label="报损日期：" label-width="120px">
                            <el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd" v-model="ruleForm.time" style="width: 300px;"></el-date-picker>
                        </el-form-item>
                        <el-form-item label="报损数量：" prop="lossNum" label-width="120px">
                            <el-input v-model.number="ruleForm.lossNum" type="number" @change="changeNum" style="width:300px">
                                <template slot="append">
                                    {{ruleForm.lossUnits}}
                                </template>
                            </el-input>
                        </el-form-item>
                        <el-form-item label="报损人：" prop="name" label-width="120px">
                            <el-select v-model="ruleForm.name" filterable placeholder="请选择" @change="changeRelevant" style="width:300px">
                                <el-option
                                    v-for="item in userList"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item"
                                >
                                {{item.name}}
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="submitForm('ruleForm')">确 定</el-button>
            </div>
        </el-dialog>

        <el-dialog title="调整产量" :visible.sync="dialogFormVisible1">
            <el-form :model="ruleForm1" :rules="rules1" ref="ruleForm1">
                <el-row>
                    <el-col :span="16">
                        <el-form-item label="调整日期：" label-width="120px">
                            <el-date-picker type="date" placeholder="选择日期" value-format="yyyy-MM-dd" v-model="ruleForm1.time" style="width: 300px;"></el-date-picker>
                        </el-form-item>
                        <el-form-item label="实际产量：" prop="newNumber" label-width="120px">
                            <el-input v-model.trim="ruleForm1.newNumber" style="width:300px">
                                <template slot="append">
                                    {{ruleForm1.units}}
                                </template>
                            </el-input>
                        </el-form-item>
                        <el-form-item label="操作人：" prop="relevantUser" label-width="120px">
                            <el-select v-model="ruleForm1.relevantUser" filterable placeholder="请选择" @change="changeRelevant1" style="width:300px">
                                <el-option
                                    v-for="item in userList"
                                    :key="item.id"
                                    :label="item.name"
                                    :value="item"
                                >
                                {{item.name}}
                                </el-option>
                            </el-select>
                        </el-form-item>
                    </el-col>
                </el-row>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible1 = false">取 消</el-button>
                <el-button type="primary" @click="submitForm1('ruleForm1')">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import {user_findAll,warehouse_findById,loss_save,manufacture_findById} from "@/request/api";
    export default {
        data() {
            return {
                searchName1: '',
                dialogFormVisible: false,
                dialogFormVisible1: false,
                ruleFormView: {},
                tableData: [],
                total1: 0,
                currentPage1:1,
                pagesize1:10,
                ruleForm: {
                    name: '',
                    lossNum: '',
                    lossUnits: ''
                },
                rules:{
                    name: [
                        { required: true, message: '不能为空', trigger: 'change' }
                    ],
                    lossNum: [
                        { required: true, message: '不能为空', trigger: 'change' }
                    ]
                },
                userList: [],
                surplusNum: 0,
                ruleForm1: {
                    time: '',
                    relevantUser: '',
                    relevantUserIds: '',
                    newNumber: '',
                    units: '',
                },
                rules1:{
                    relevantUser: [
                        { required: true, message: '不能为空', trigger: 'change' }
                    ],
                    newNumber: [
                        { required: true, message: '不能为空', trigger: 'blur' }
                    ]
                },
            };
        },
        mounted(){
            this.id = this.$route.query.id
            this.init1()
        },
        methods: {
            backPage () {
                this.$router.go(-1);
            },
            init1(){
                // warehouse_findById(this.$route.query.id).then((res) =>{
                //     this.ruleFormView = res.data
                //     this.tableData = res.data.materialList
                // })
                // 仓库信息和库存信息列表
                warehouse_findById({
                  id: this.id,
                  searchName: this.searchName1,
                  page: this.currentPage1,
                }).then((res) =>{
                    this.ruleFormView = res.data.warehouse
                    this.tableData = res.data.list.rows
                })
            },
            // 用户列表
            userListFun() {
                user_findAll('').then((res)=>{
                    this.userList = res.data
                })
            },
            // 选择报损相关人
            changeRelevant(ele){
                this.ruleForm.userId = ele.id
                this.ruleForm.name = ele.name
            },
            // 报损数量
            changeNum(ele){
                if (ele <= 0) {
                    this.$notify.error({
                        title: '提示',
                        message: '报损数量不能为零或负数！',
                        showClose: false,
                        duration:2000
                    });
                    return this.ruleForm.lossNum = 0
                }
                if (ele > this.surplusNum) {
                    this.$notify.error({
                        title: '提示',
                        message: '报损数量不能大于剩余量！',
                        showClose: false,
                        duration:2000
                    });
                    return this.ruleForm.lossNum = 0
                }
            },
            //报损
            handleFrmLoss(index,row){
                this.dialogFormVisible = true
                this.userListFun();
                this.ruleForm.materialId = row.id
                this.ruleForm.lossUnits = row.units
                this.surplusNum = parseInt(row.surplusNum)
            },
            // 提交报损
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        loss_save(this.ruleForm).then((res) =>{
                            this.ruleForm = {}
                            this.$notify({
                                title: '提示',
                                message: '保存成功',
                                type: 'success',
                                duration:1000
                            });
                            this.dialogFormVisible = false
                            if (this.materialType == '中药材') {
                                this.init1()
                            } else if(this.materialType == '辅料') {
                                this.init2()
                            } else if(this.materialType == '提取饮片'){
                                this.init3()
                            } else if(this.materialType == '配方颗粒'){
                                this.init4()
                            }
                        })
                    } else {
                        return false;
                    }
                })
            },
            // 查看
            handleView(index,row){
                if (row.type == '原料') {
                    this.$router.push({
                        path: '/materialCheck/viewMaterialCheck',
                        query: { id: row.id }
                    });
                } else {
                    this.$router.push({
                        path: '/production/viewProduction',
                        query: { id: row.id }
                    });
                }
            },
            // 调整产量
            handleNum(index,row){
                this.dialogFormVisible1 = true
                this.userListFun();
                manufacture_findById(row.id).then((res) =>{
                    // console.log(res)
                    this.ruleForm1 = res.data
                    this.ruleForm1.relevantUser = res.data.relevantUsers
                })
            },
            // 选择调整产量相关人
            changeRelevant1(ele){
                this.ruleForm1.userId = ele.id
                this.ruleForm1.name = ele.name
            },
            // 提交产量
            submitForm1(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        // console.log(typeof this.ruleForm.userNumber)
                        if (parseInt(this.ruleForm1.newNumber) > parseInt(this.ruleForm1.userNumber)) {
                            var parm = {
                                manufactureId: this.ruleForm1.id,
                                time: this.ruleForm1.time,
                                relevantUsers: this.ruleForm1.relevantUsers,
                                relevantUserIds: this.ruleForm1.relevantUserIds,
                                newNumber: this.ruleForm1.newNumber,
                                units: this.ruleForm1.units,
                            }
                            manufacture_adjust(parm).then((res) => {
                                this.$notify({
                                    title: '提示',
                                    message: '添加成功',
                                    type: 'success',
                                    duration:1000
                                });
                                this.dialogFormVisible1 = false
                                if (this.materialType == '提取饮片') {
                                    this.init1()
                                } else if(this.materialType == '辅料') {
                                    this.init2()
                                } else if(this.materialType == '提取饮片'){
                                    this.init3()
                                } else if(this.materialType == '配方颗粒'){
                                    this.init4()
                                }
                            })
                        } else{
                            this.$notify.error({
                                title: '提示',
                                message: '实际产量不能小于剩余量'+this.ruleForm1.userNumber+'',
                                showClose: false,
                                duration:3000
                            });
                        }
                    } else {
                        return false;
                    }
                })
            },
            // 搜索
            handleSearch1() {
                this.init1()
            },
            handleCurrentChange1(val){
                this.currentPage1 = val
                this.init1()
            },
        }
    }
</script>

<style scoped lang="less">
.el-pagination{
    text-align: center;
}
.demo-ruleForm{
    margin-top: 10px;
}
.demo-ruleForm .el-radio--small.is-bordered{
    width: 120px !important;
    margin-right: 10px !important;
}
</style>